package com.xmh.code.generation.filecrate;



import com.xmh.code.generation.inject.Inject;
import com.xmh.code.generation.util.CreateUtils;
import com.xmh.code.generation.util.FileUtil;
import com.xmh.code.generation.util.NameUtils;
import com.xmh.code.generation.vo.TableVo;

import java.io.*;
import java.util.List;
import java.util.Map;

import static java.lang.String.format;

public class ModeFileCreate {


    /**
     * 根据模板文件创建对应的文件以及对应路径
     *
     * @param tableList
     */
    @SuppressWarnings("unchecked")
    public static void createLogicFilesByMode(List<TableVo> tableList) {
        for (Map.Entry<String, String> entry : Inject.getLogicFiles().entrySet()) {
            String modeCode = getModelCode(entry.getKey());

            for (TableVo tableVo : tableList) {
                // 转为驼峰
                String voName = NameUtils.nameTransformation(tableVo.getName());
                // 首字母大写
                String className = NameUtils.titleCase(voName);

                String fileName = className + entry.getKey();

                String code = NameUtils.extracted(voName, className, modeCode);

                CreateUtils.outFile(code, entry.getValue(), fileName, "java");
            }
        }
    }

    private static String getModelCode(String key) {
        return FileUtil.getSourceFile(ModeFileCreate.class, "/model/SBM/" + key + ".model");
    }

    public static void createConfigFilesByMode() {
        Map<String, String> configFileMap = Inject.getConfigFiles();
        for (Map.Entry<String, String> entry : configFileMap.entrySet()) {

            String code = getModelCode(entry.getKey());

            String extracted = NameUtils.extracted("", "", code);

            CreateUtils.outFile(extracted, entry.getValue(), entry.getKey(), "java");
        }
    }

    @SuppressWarnings("unchecked")
    private static void createModeApp(List<Object> tableList, String modeCode, String path, String nameSuffix) {
        for (Object object : tableList) {
            Map<String, Object> tableMap = (Map<String, Object>) object;
            // 转为驼峰
            String voName = NameUtils.nameTransformation((String) tableMap.get("tableName"));
            // 首字母大写
            String className = NameUtils.titleCase(voName);

            String fileName = "FinanceService";

            String code = NameUtils.extracted(voName, className, modeCode);

            try {
                File file = new File(format("%s//%s.java", path, fileName));

                if (!file.exists()) {
                    new File(path).mkdirs();
                    try {
                        file.createNewFile();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

                String txtFile = "";

                BufferedReader bufferedReader = new BufferedReader(new FileReader(file));

                String line = bufferedReader.readLine();

                while (line != null) {
                    txtFile += line + "\n";
                    line = bufferedReader.readLine();
                }

                bufferedReader.close();

                StringBuilder sBuffer = new StringBuilder(txtFile.replace("}", ""));

                String string = sBuffer.append(code).toString();

                OutputStreamWriter write = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
                BufferedWriter writer = new BufferedWriter(write);
                writer.write(string);
                writer.flush();
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
